import http from '../../util/request.js';

//获取应用实例
const app = getApp()
Page({
  data: {
    pushList: []
  },
  onLoad: function() {
    var that = this;
    that.setTabBar();
    that.getTips();
  },
  // 获取卡片
  getTips: function () {
    var that = this;
    http.get('tips', {
      act: 'list'
    }).then(function (res) {
      that.setData({
        pushList: res
      })
    });
  },
  setTop: function (setting) {
    var that = this;
    var backgroundColor = setting.pro.backgroundColor != undefined ? setting.pro.backgroundColor : '#000000';
    var frontColor = setting.pro.frontColor != undefined ? setting.pro.frontColor : '#ffffff';
    var currentPage = getCurrentPages();
    var page = currentPage[0].route;
    wx.setNavigationBarColor({
      frontColor: frontColor,
      backgroundColor: backgroundColor
    });
    var tarBar = that.data.blist;
    for (let key in tarBar) {
      if (tarBar[key].link == page) {
        wx.setNavigationBarTitle({
          'title': tarBar[key].title
        });
        break;
      }
    }
    that.setData({
      currentPage: page
    });
  },
  // 设置底部导航
  setTabBar: function () {
    var that = this;
    var cache = wx.getStorageSync("barlist");
    var setting = {};
    if (cache) {
      setting = cache;
      that.setData({
        blist: cache.menu,
        pro: cache.pro
      });
      that.setTop(setting);
    } else {
      http.get('quickmenu', {}).then(function (res) {
        setting = res;
        console.log(setting);
        if (res != {}) {
          that.setTop(setting);
          wx.setStorageSync('barlist', res);
          that.setData({
            blist: res.menu,
            pro: res.pro
          });
        } else {
          that.setData({
            blist: [],
            pro: []
          });
        }
      });
    }
  },
  handleSwipeOut(...args) {
    var that = this;
    if (args[0].detail.list.length == 1) {
      http.get('tips', {
        act: 'list'
      }).then(function(res) {
        that.setData({
          pushList: res
        })
      });
    }
  },
  handleClickCard(...args) {
    //console.log(args)
  },
  receive: function(e) {
    var id = e.detail.id;
    http.get('tips', {
      act: 'receive',
      id: id
    }).then(function(res) {
      wx.showToast({
        title: '收藏成功',
      })
    });
  },
  like: function(e) {
    var id = e.detail.id;
    http.get('tips', {
      act: 'like',
      id: id
    }).then(function(res) {
      wx.showToast({
        title: '点赞成功',
      })
    });
  }
})